<?php

namespace extend;


use app\model\Settings;
use Nette\Mail\SmtpMailer;
use Nette\Mail\Message;

class Mail
{
    public static function send($to = "", $text = ""): bool
    {
        $siteName = Settings::Config("app_name", "");
        $template = <<<EOF
                        <h2>$siteName</h2>
                        <p>
                           <span>您正在登录$siteName,您的动态令牌为</span>
                           <strong style='color:#4b9fff' >$text</strong>
                           <span>有效期10分钟</span>
                        </p>
                    EOF;
        $userName = env('mail.account');
        $mail = new Message;
        $mail->setFrom("$siteName <$userName>")->addTo($to)->setSubject($siteName)->setHtmlBody($template);
        $mailer = new SmtpMailer([
            'host' => env('mail.smtp', ''),
            'username' => $userName,
            'password' => env('mail.password'),
            'secure' => 'ssl',
        ]);
        try {
            $mailer->send($mail);
        } catch (\Throwable $th) {
            //throw $th;
            return false;
        }
        return true;
    }
}
